package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.InvitationJoin;
import com.tbit.uqbike.client.pojo.InviteNewEvent;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.UserRewardLog;
import com.tbit.uqbike.client.service.InviteNewEventService;
import com.tbit.uqbike.client.service.RedisService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.service.UserRewardLogService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.tbit.uqbike.client.constant.RedisConstant.*;

/**
 * 邀请新用户活动
 *
 * @Author: chen
 * @Description: 邀请新用户活动
 * @Date: 创建于 15:26 2020/12/31
 */
@Controller
@RequestMapping("/newEvent")
public class InviteNewEventController {

    @Autowired
    private InviteNewEventService inviteNewEventService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserRewardLogService userRewardLogService;
    @Autowired
    private RedisService redisService;



    /**
     * 通过区域id和类型获取
     *
     * @param token       令牌
     * @param adAccountId 区域id
     * @param type        类型
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/getByAccountId")
    public void getByAccountId(String token, Integer adAccountId, @RequestParam(defaultValue = "0") Integer type, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(adAccountId, "-3050", "区域id不能为空");
            User user = tokenService.getUser(token);
            InviteNewEvent inviteNewEvent = inviteNewEventService.getNewEvent(adAccountId, type, user);
            if (inviteNewEvent != null) {
                Integer total = userRewardLogService.getUserCount(user.getUserId(), inviteNewEvent.getInviteId());
                map.put("total", total == null ? 0 : total);
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, inviteNewEvent);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 通过邀请码获取活动信息
     *
     * @param token    令牌
     * @param code     邀请码
     * @param type     类型
     * @param request  请求
     * @param response 响应
     */
    @RequestMapping("/getByCode")
    public void getByCode(String token, String code, @RequestParam(defaultValue = "0") Integer type, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(code, "-180004", "邀请码不能为空");
            User user = tokenService.getUser(token);
            InviteNewEvent inviteNewEvent = inviteNewEventService.getByCode(code, type, user);
            if (inviteNewEvent != null) {
                Integer total = userRewardLogService.getUserCount(user.getUserId(), inviteNewEvent.getInviteId());
                map.put("total", total == null ? 0 : total);
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, inviteNewEvent);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 用户邀请记录统计
     *
     * @param token    令牌
     * @param code     邀请码
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/getByInviteId")
    public void getByInviteId(String token, String code, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(code, "-180004", "邀请码不能为空");
            User user = tokenService.getUser(token);
            List<InvitationJoin> invitationJoins = inviteNewEventService.getUserEventLog(code, user.getUserId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, invitationJoins);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取用户赠送记录
     *
     * @param token    令牌
     * @param code     邀请码
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/getUserRewardLog")
    public void getUserRewardLog(String token, String code, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(code, "-180004", "邀请码不能为空");
            User user = tokenService.getUser(token);
            List<UserRewardLog> userEventLogs = userRewardLogService.getByCode(code, user.getUserId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, userEventLogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取用户小程序邀请码
     *
     * @param token
     * @param code
     * @param request
     * @param response
     */
    @RequestMapping("/createQrcode")
    public void createQrcode(String token, String code, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(code, "-180004", "邀请码不能为空");
            User user = tokenService.getUser(token);
            String data = inviteNewEventService.createQrcode(user, code);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, data);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 分享次数统计
     * @param eventId 邀新活动id
     */
    @PostMapping("/share-count")
    public void shareCount(String token, Integer eventId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            boolean res = redisService.hash_incr(getInviteNewRedisKey(eventId, user.getPhone(), user.getName()), SHARE, 1L);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA,res);
        }catch (BaseException e){
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 曝光次数统计
     * @param eventId 邀新活动id
     */
    @PostMapping("/exposure-count")
    public void exposureCount(String token, Integer eventId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            boolean res = redisService.hash_incr(getInviteNewRedisKey(eventId, user.getPhone(), user.getName()), EXPOSURE, 1L);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA,res);
        }catch (BaseException e){
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取邀新活动的Key
     * @param eventId
     * @param phone
     * @param name
     * @return
     */
    private String getInviteNewRedisKey(Integer eventId,String phone,String name){
        String key = String.format(INVITE_NEW, eventId, phone, name);
        redisService.lPushWithStr(INVITE_NEW_KEYS,key);
        return key;
    }

}
